<template>
  <el-dialog :title="dialog.title" :visible.sync="dialog.show" width="530px" height="400px" custom-class="mydialog" append-to-body @close="handleClose">
    <div class="dialog-content">
      <el-form ref="form1" :model="form" :rules="rules" label-width="150px" >
        <el-form-item label="出货仓：" >
          <el-select v-model="selectWarehouseValue" placeholder="选择出货仓" class="seach-select mb-30" size="small">
            <el-option v-for="item in warehouseList" :key="item.warehouseId" :label="item.warehouseName" :value="item.warehouseId"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="text-center mt-30">
        <el-button class="greenbt" type="primary" @click="branchSubmit">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { get, post } from '@/api/utils'
import { message } from '@/utils/utils'
export default{
  props: {
    modalStaticData: {
      type: Object,
      default: () => {}
    },
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialog: {
        title: '', show: true
      },
      form: {
        type_options: [], type: ''
      },
      warehouseList: [], selectWarehouseValue: null,

      rules: {}

    }
  },
  created() {
    this.getWarehouseList()
  },
  methods: {

    getWarehouseList() {
      var data = { 'deleteFlag': 'N', 'offset': '0', 'status': 1 }

      get('/wxh-cloud-rest/rest/', '/pas/warehouse/queryAllWarehouse', data).then(response => {
        if (response.data.status === 'success') {
          if (response.data.data.list && response.data.data.list.length > 0) {
            this.warehouseList = response.data.data.list.filter((warehouse) => warehouse !== null)
          }
        } else if (response.data.status === 'token_check_faild') {
          message(this, response.data.messge)
        }
      })
        .catch(error => {
          console.log(error)
          message(this, '数据加载失败')
        })
    },

    branchSubmit() {
      if (this.selectWarehouseValue && this.selectWarehouseValue > 0) {
        if (this.modalStaticData && this.modalStaticData.selectMallOrderItemList && this.modalStaticData.selectMallOrderItemList.length > 0) {
          var mallOrderItemIds = this.modalStaticData.selectMallOrderItemList.map((mallOrderItem) => { return mallOrderItem.mallOrderItemId }).join(',')
          var data = { 'warehouseId': this.selectWarehouseValue, mallOrderItemIds }

          post('/wxh-cloud-rest/rest/', '/pas/mallOrder/mallOrder', data, { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept': 'application/json' }).then(response => {
            if (response.data.status === 'success') {
              console.log(response)
              message(this, '分仓成功', 'success')
              this.modalStaticData.closeChildAndRefresh()
            } else if (response.data.status === 'token_check_faild') {
              message(this, response.data.messg)
            }
          }).catch(error => {
            console.log(error)
            message(this, '数据加载失败')
          })
        }
      } else {
        message(this, '请选择要分仓的仓库')
      }
    },

    handleClose() {
      this.$emit('close')
    }

  }
}
</script>
